/* #DEFINEs */

#define    toxdigit(v) ((v)<10?(v)+'0':(v)+55)
#define    fromxdigit(b) (isxdigit(b)?(b)-'0':(b)-55)
#define MYSIGN(x)  (x < 0 ? -1 : 1/*x > 0 ? 1 : 0*/)
#define MEMDIFF(res, s, d, no)  {for(n=0, res=0; n<no && !res; n++) \
											res = /*MYSIGN*/(s[n] - d[n]);}


#define    NFH_NO_PAD          0
#define    NFH_LEFT_PAD        1
#define    NFH_RIGHT_PAD       2

#define    NFH_NO_COMPRESS     0
#define    NFH_BCD_COMPRESS    1
#define    NFH_MODIFY_COMPRESS 2
#define    NFH_USER_COMPRESS   4

#define    NFH_BC_SEARCH       0
#define    NFH_BC_WEND_SEARCH  1
#define    NFH_BC_WMID_SEARCH  2
#define    NFH_USER_SEARCH     4

#define    NFH_NO_PRE_INIT     0
#define    NFH_PRE_INIT        1




          /* Insert Seed with Seed Size = Max Pan Size in Master File */


/* STRUCTs */


// @struct TagNFH_MIT_struct | This structrue formater data
typedef struct TagNFH_MIT_struct
{
   int     size;                // @field Size
   int     master_number;       // @field Master Table number
   char    pad_char;            // @field Pad char
   char    compress_constant;   // @field Compression constant
   int     seed_size;           // @field Seed size
   int     max_pan_size;        // @field Maximum PAN length
   int     decomp_ratio;        // @field Decompression ratio
   int     sit_data_size;       // @field SIT data size
   char    wildcard_char;       // @field Wildcard character
   char    pre_init_flag;       // @field Pre initialisation flag
   char    compress_flag;       // @field Compression flag
   char    pad_flag;            // @field Pad flag

}NFH_MIT_struct;


// @struct TagNFH_MIT_struct | This structrue formater data
typedef struct TagNFH_par_struct
{
   int     size;            // @field size
   int     master_number;   // @field manster table number
   char    pad_flag;        // @field pad flag
   char    compress_flag;   // @field compress flag
   char    search_flag;     // @field Search flag
   char    *index;          // @field Seed or PAN residue (ie PAN-seed)
   char    *data;           // @field Data pointer
   char    (*u_comp)(char, int*, char*, int*, char*);// @field user-defined compress/decompress
   char    (*u_search)( char*, char*, int );// @field user-defined search function
}NFH_par_struct;




/* MACROS */

#define NFH_MIT_SET( str, mn, pc, cc, ss, mp, dr, ds, wc, pi, cf, pf) \
   str.size = sizeof(str);     \
   str.master_number = mn;     \
   str.pad_char = pc;          \
   str.compress_constant = cc; \
   str.seed_size = ss;         \
   str.max_pan_size = mp;      \
   str.decomp_ratio = dr;      \
   str.sit_data_size = ds;     \
   str.wildcard_char = wc;     \
   str.pre_init_flag = pi;     \
   str.compress_flag = cf;     \
   str.pad_flag = pf;



#define NFH_PAR_SET( str, mn, pf, cf, sf, id, dt, uc, us ) \
   str.size = sizeof(str);     \
   str.master_number = mn;     \
   str.pad_flag = pf;          \
   str.compress_flag = cf;     \
   str.search_flag = sf;       \
   str.index = id;             \
   str.data = dt;              \
   str.u_comp = uc;            \
   str.u_search = us;





// @struct TagNFH_MIT_Header | This structrue formater data
typedef struct TagNFH_MIT_Header
{
   int     size, master_number; // @field Size, Master table number
   char    pad_char;            // @field Pad char
   char    compress_constant;   // @field Compression constant
   int     seed_size;           // @field Seed size
   int     max_pan_size;        // @field Maximum PAN size
   int     decomp_ratio;        // @field Decompression Ratio
   int     sit_data_size;       // @field SIT data size
   char    wildcard_char;       // @field Wild Card
   char    pre_init_flag;       // @field Pre initialisation flag
   char    compress_flag;       // @field Compression Flag
   char    pad_flag;            // @field Pad flag
   int     seed_counter;        // @field Seed counter
}NFH_MIT_Header;
#define SIZE_NFH_MIT_HEADER sizeof(NFH_MIT_Header)



char NFH_init_master(NFH_MIT_struct *MIT_struct,  char MIT_update_flag);
char NFH_init_seed( NFH_par_struct  *NFH_par_struct );
char NFH_add_card( NFH_par_struct *NFH_par_struct );
char NFH_update_card(NFH_par_struct *NFH_par_struct );
char NFH_retrieve_card(NFH_par_struct *NFH_par_struct );
char NFH_delete_card(NFH_par_struct *NFH_par_struct );
char NFH_delete_seed(NFH_par_struct *NFH_par_struct );
char NFH_delete_master(NFH_par_struct *NFH_par_struct );

int NFH_pad(char *in_str, char *out_str, int len, char pad_char, char pad_flag);
char NFH_compress(int op_flag, int *in_size, char *in_str, int *out_size, char* out_str);
static int open_data_file(int mit_num, int sit_num, int open_mode);

/* MBB */
char indexed_update(int handle, int start_off, int rec_size, int field_size, 
			        int field_off, char *index, char *data, char(*func)(char*,char*,int));

char indexed_del(int handle, int start_off, int rec_size, int field_size, 
			     int field_off, char *index, char *data, char(*func)(char*,char*,int));

char indexed_add(int handle, int start_off, int rec_size, int field_size, 
			     int field_off, char *index, char *data, char(*func)(char*,char*,int));

char indexed_find(int handle, int start_off, int rec_size, int field_size, int field_off, 
				  char *index, long *pos, char(*func)(char*, char*, int));

/* MBB */

#define NFH_SUCCESS          1


#define ERR_INDEX_SIZE       -1
#define ERR_MAXPAN_SIZE      -2
#define ERR_MIT_NUMBER       -3
#define ERR_MIT_NOTFOUND     -4
#define ERR_SEED_NOTFOUND    -5
#define ERR_DATA_DIFFER      -6
#define ERR_SEED_COUNT       -7
#define ERR_PAN_NOTFOUND     -8
#define ERR_SAME_PC_WC       -9

#define ERR_SIT_NOTFOUND     -10
#define NFH_MEMORY_ERROR	 -11

#define ERR_UNDEFINED        -99

#define ERR_HEADER_WRITE     -30
#define ERR_READ_FILE        -31
#define ERR_WRITE_FILE       -32
#define ERR_SEED_SIZE        -33

